﻿Imports Microsoft.VisualBasic

Public Class clsUser
    Public UserID As String
    Public UOrganizationID As String
    Public UserType As My.UserType
    Public RegionID As String
    Public RegionType As My.RegionType
    Public VolunteerLeader As Boolean

    'For 志工
    Public VolunteerStatus As My.VolunteerStatus

    Public Sub New()
        Me.UserID = clsAuth.GetUserID()

        'If HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Contains(FormsAuthentication.LoginUrl.ToLower()) _
        '    Or TryCast(HttpContext.Current.Handler, Page).MasterPageFile.Contains("Public") Then

        '    Me.UserType = New My.UserType(HttpContext.Current.Response.Cookies("UserTypeValue").Value)
        '    Me.UOrganizationID = HttpContext.Current.Response.Cookies("UOrganizationID").Value

        'Else
        Me.UserType = New My.UserType(HttpContext.Current.Request.Cookies("UserTypeValue").Value)
        Me.UOrganizationID = HttpContext.Current.Request.Cookies("UOrganizationID").Value
        Me.VolunteerStatus = New My.VolunteerStatus(HttpContext.Current.Request.Cookies("VolunteerStatus").Value)
        Me.RegionID = HttpContext.Current.Request.Cookies("RegionID").Value
        Me.RegionType = New My.RegionType(HttpContext.Current.Request.Cookies("RegionType").Value)

        If HttpContext.Current.Request.Cookies("VolunteerLeader") IsNot Nothing AndAlso HttpContext.Current.Request.Cookies("VolunteerLeader").Value = "Y" Then
            Me.VolunteerLeader = True
        End If

        'End If

    End Sub

    Public Shared Function CanDeleteVolunteer(ByVal volunteerID As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select top 1 Id from TrainingRegister ")
        sql.AppendFormat("where VolunteerID = '{0}' ", volunteerID)
        sql.AppendFormat("union select top 1 ApplicationID from EnlistRecordApplication ")
        sql.AppendFormat("where VolunteerID = '{0}' ", volunteerID)
        sql.AppendFormat("union select top 1 1 from VBook ")
        sql.AppendFormat("where VolunteerID = '{0}' ", volunteerID)
        'sql.AppendFormat("union select top 1 1 from VOrganizationMapping ")
        'sql.AppendFormat("where VolunteerID = '{0}' ", volunteerID)

        If dat.GetData(sql).Rows.Count > 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    ''' <summary>
    ''' 預設密碼 = 身份證後2碼 + 生日 MMDD
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function RestorePassword(ByVal volunteerID As String, ByVal birthday As DateTime) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim password As String = volunteerID.Substring(volunteerID.Length - 2) + birthday.ToString("MMdd")

        Using scope As New Transactions.TransactionScope
            sql.AppendFormat("update Volunteer ")
            sql.AppendFormat("set Password = '{0}' ", clsAuth.EncryptPassword(password))
            sql.AppendFormat("where VolunteerID = '{0}' ", volunteerID)

            dat.AccData(sql)

            scope.Complete()
        End Using

        Return True
    End Function

End Class
